#/usr/bin/python
import sys
from crypto import parsePlainFile, parseVectorFile, checkMod, checkMult, encrypt, writeCipherFile, writeVectorFile

from traceback import print_exception
#encrypt with the knapsack cipher		
try:
	#get file to encrypt
	input_fname = raw_input("Enter the name of the file to encrypt:  ")
	
	#get file into a string
	plain_text = parsePlainFile(input_fname)
	
	#get super increasing set
	super_fname = raw_input("Enter the name of the file with the encryption set: ")
	
	#parse the file into a list of numbers
	super_set = parseVectorFile(super_fname)

	goodMod = False
	mod = 0
	#ask for the modulus
	while not goodMod:
		mod = int(raw_input("Enter the number to be used as the modulus:  "))
	
		goodMod = checkMod(mod, super_set)
		if not goodMod:
			print "Invalid modulus input!"
	
	goodMult = False
	mult = 0
	#ask for a multiplier
	while not goodMult:
		mult = int(raw_input("Enter the number to be used as the multiplier:  "))
		
		goodMult = checkMult(mult, mod, super_set)
		if not goodMult:
			print "Invalid multiplier input!"
			
	#get the output file
	output_fname = raw_input("Enter the name of the file to output the cipher to:  ")
	
	#encrypt file and write to output file
	cipher = encrypt(plain_text, super_set, mod, mult)
	
	writeCipherFile(output_fname, cipher)
	
	vector_output = raw_input("Enter the name of the file to output the hard vector to.:  ")
	
	writeVectorFile(vector_output, (((i * mult) % mod) for i in super_set))
	
except KeyboardInterrupt:
	print()
	sys.exit()
except:
	print "You fail!! The code threw and exception."
	e = sys.exc_info()
	print_exception(e[0], e[1], e[2])
